<template>
  <div class="container">
    <div class="header">
      <div class="input-group">
        <label class="input-label">方法名称</label>
        <input v-model="info.name" placeholder="方法名称" class="input-field" />
      </div>
      <div class="input-group">
        <label class="input-label">调用方法</label>
        <input v-model="info.path" placeholder="调用方法" class="input-field" />
      </div>
      <div class="input-group">
        <label class="input-label">nacos地址</label>
        <input v-model="info.address" placeholder="nacos地址" class="input-field" />
      </div>
      <div class="input-group">
        <label class="input-label">是否鉴权</label>
        <select v-model="info.isauth" class="input-field">
          <option value="true">是</option>
          <option value="false" selected>否</option>
        </select>
      </div>
      <div class="input-group" v-if="isAuthEnabled">
        <label class="input-label">用户名</label>
        <input v-model="info.username" placeholder="用户名" class="input-field" />
      </div>
      <div class="input-group" v-if="isAuthEnabled">
        <label class="input-label">密码</label>
        <input type="password" v-model="info.password" placeholder="密码" class="input-field" />
      </div>
      <div class="input-group">
        <label class="input-label">超时时间</label>
        <input v-model="info.timeout" placeholder="超时时间" class="input-field" />
      </div>
      <button @click="testrpcprovider" class="add-row-button">测试服务是否正常</button>
      <button @click="addRow" class="add-row-button">新增行</button>
    </div>
    <div class="table-container">
      <table class="editable-table">
        <thead>
        <tr>
          <th>字段名称</th>
          <th>测试值</th>
          <th>参数类型</th>
          <th>描述</th>
          <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(row, index) in info.paramData" :key="index">
          <td><input v-model="row.fieldName" class="table-input" /></td>
          <td><input v-model="row.testValue" class="table-input" /></td>
          <td>
            <select v-model="row.paramType" class="table-select">
              <option value="数值">数值</option>
              <option value="字符串">字符串</option>
              <option value="集合">集合</option>
              <option value="Map">Map</option>
              <option value="object">object</option>
            </select>
          </td>
          <td><input v-model="row.description" class="table-input" /></td>
          <td><button @click="removeRow(index)" class="action-button">删除</button></td>
        </tr>
        </tbody>
      </table>
    </div>
    <!-- Modal -->
    <div v-if="showModal" class="modal-overlay" @click.self="closeModal">
      <div class="modal-content" :class="`modal-${messageType}`">
        <span class="close-button" @click="closeModal">&times;</span>
        <div class="icon"></div>
        <p>{{ modalMessage }}</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { inject,ref, computed } from 'vue'

const $i = inject('i18n.format')
const info = inject('info')
const addRow = () => {
  if (!Array.isArray(info.value.paramData)) {
    info.value.paramData = [];
  }
  info.value.paramData.push({ fieldName: '', testValue: '', paramType: '数值', description: '' });
};

const removeRow = (index) => {
  if (info.value.paramData.length >= 1) {
    info.value.paramData.splice(index, 1);
  }
};

const showModal = ref(false)
const modalMessage = ref('')
const messageType = ref('')

const testrpcprovider = () => {
  //弹窗提醒
  fetch('/magic/web/rpcprovider/test',{
    method: 'post',
    body: JSON.stringify({
      info: info.value
    }),
    headers: {
      'Content-Type': 'application/json'
    }
  }).then(function(data){
    // text()方法属于fetchAPI的一部分 它返回一个Promise实例对象 用于获取后台返回的数据
    return data.text();
  }).then(function(data){
    if(data.code === '1'){
      showModal.value = true;
      modalMessage.value = "测试成功！"
      //success,error,info
      messageType.value = "success"
    }else{
      showModal.value = true;
      modalMessage.value = "测试失败！"+data.message
      //success,error,info
      messageType.value = "error"
    }
    console.log(data);
  })
}


const closeModal = () => {
  showModal.value = false
}

const isAuthEnabled = computed(() => {
  return info.value.isauth === "true";
});

</script>

<style scoped>
.container {
  width: 100%;
  font-family: Arial, sans-serif;
}

.header {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-left: 10px;
  margin-bottom: 10px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 30px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:checked + .slider:before {
  transform: translateX(22px);
}

.input-group {
  display: flex;
  align-items: center;
  margin-left: 10px;
}

.input-label {
  margin-right: 5px;
  font-weight: bold;
  color: #333;
  font-size: 12px;
}

.input-field {
  padding: 6px 10px;
  font-size: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  width: 150px;
}

.table-container {
  margin-top: 10px;
  max-height: 150px; /* 设置最大高度 */
  overflow-y: auto; /* 添加垂直滚动条 */
  border: 1px solid #ddd; /* 添加边框以区分表格 */
  position: relative; /* 相对定位 */
}

.editable-table {
  width: 100%;
  border-collapse: collapse;
  border: none; /* 去掉表格边框 */
  font-size: 12px;
}

.editable-table th {
  background-color: #f8f8f8;
  color: #333;
  padding: 6px;
  text-align: left;
  border-bottom: 1px solid #ddd;
  border-right: 1px solid #ddd;
  position: sticky; /* 固定表头 */
  top: 0;
  /* z-index: 1; 确保表头在上方 */
}

.editable-table th:last-child {
  border-right: none;
}

.editable-table td {
  padding: 0;
  border-bottom: 1px solid #ddd;
  border-right: 1px solid #ddd;
}

.editable-table td:last-child {
  border-right: none;
}

.table-input, .table-select {
  width: 100%;
  height: 100%;
  padding: 6px;
  font-size: 12px;
  border: none;
  box-sizing: border-box;
}

.table-select {
  appearance: none; /* 去掉默认的下拉箭头 */
  -webkit-appearance: none;
  -moz-appearance: none;
  background: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 5'%3E%3Cpath fill='%23333' d='M2 0L0 2h4zm0 5L0 3h4z'/%3E%3C/svg%3E") no-repeat right 8px center;
  background-size: 10px 10px;
}

.action-button {
  width: 100%;
  height: 100%;
  padding: 6px;
  background-color: #ff4d4d;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 12px;
  box-sizing: border-box;
}

.action-button:hover {
  background-color: #e64a4a;
}

.add-row-button {
  margin-left: 20px;
  padding: 6px 12px;
  background-color: #2196F3;
  color: white;
  border: none;
  cursor: pointer;
  font-size: 12px;
  box-sizing: border-box;
}
.add-row-button:hover {
  background-color: #0b7dda;
}


/* Modal styles */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
  width: 80%;
  max-width: 400px;
  display: flex;
  align-items: center;
}

.close-button {
  position: absolute;
  top: 10px;
  right: 10px;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
}

.icon {
  width: 30px;
  height: 30px;
  margin-right: 10px;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

.modal-success .icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='9' fill='%23f6ffed' stroke='%2352c41a' stroke-width='1.5'/%3E%3Cpath fill='none' stroke='%2352c41a' stroke-width='2' stroke-linecap='round' d='M7 12l4 4 6-8'/%3E%3C/svg%3E");
}

.modal-error .icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='9' fill='%23ffebe9' stroke='%23ff4d4f' stroke-width='1.5'/%3E%3Cpath fill='none' stroke='%23ff4d4f' stroke-width='2' stroke-linecap='round' d='M8 8l8 8m0-8l-8 8'/%3E%3C/svg%3E");
}

.modal-info .icon {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='9' fill='%23fffbe6' stroke='%23faad14' stroke-width='1.5'/%3E%3Cg fill='%23faad14'%3E%3Crect x='11' y='6' width='2' height='9' rx='1'/%3E%3Ccircle cx='12' cy='17' r='1'/%3E%3C/g%3E%3C/svg%3E");
}

.modal-success {
  background-color: #ecffe7;
  border: 1px solid #4caf50;
}

.modal-error {
  background-color: #ffe7e7;
  border: 1px solid #f44336;
}

.modal-info {
  background-color: #e7efff;
  border: 1px solid #2196f3;
}
</style>




